import commJS from '../common.js';

export default {
  components: {

  },
  data() {
    return {
      //  表单
      form_data: {
        type:"",
        title:"",
        oid:"",
        user_type:"",
        head_txt:"",
        foot_txt:""
      },
      type:[
        {
          id:1,name:"杂志"
        },
        {
          id:2,name:"小课"
        },
        {
          id:3,name:"视频"
        },
        {
          id:4,name:"活动"
        }
      ],
      user_type:[
        {
          id:0,name:"全部"
        },
        {
          id:1,name:"会员"
        },
        {
          id:2,name:"非会员"
        },
      ],
      restaurants:[],
      kw:"",
      banner_id:"",
      rules:{
        type: [{
          required: true,
          message: '请选择推送类型',
          trigger: 'blur',
        }],
        title: [{
          required: true,
          message: '请选择课程名称',
          trigger: 'change',
        }],
        user_type:[{
          required: true,
          message: '请选择用户类型',
          trigger: 'blur',
        }],
        head_txt:[{
          required: true,
          message: '请输入头部文案',
          trigger: 'blur',
        }],
        foot_txt:[{
          required: true,
          message: '请输入底部文案',
          trigger: 'blur',
        }]
      },
    }
  },

  /**
   * 进入页面加载
   */
  mounted: function () {
    var that = this;

    var query = that.$route.query;
    if (query.banner_id) {
      that.banner_id = query.banner_id;
      that.getDetail();
    }
  },

  //方法
  methods: {
    /*
    * 服务器获取标题
    * */
    querySearchAsync(queryString,cb) {
      if(!this.form_data.type)return this.$message.error("请先选择推送类型");
      var that=this;
      var restaurants = that.restaurants;
       var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
       cb(results);
      }, 3000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.title.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    /*得到当前标题*/
    handleSelect(item) {
      this.$set(this.form_data,"title",item.title)
      this.$set(this.form_data,"oid",item.id)
    },
    /**
     * 获取详情
     */
    getDetail() {
      var that = this;

      that.axios.post("/notice/edit", {
        id: that.banner_id
      }).then(data => {
        if (data) {
          that.form_data.type = data.type;
          that.form_data.title = data.title;
          that.form_data.oid = data.oid;
          that.form_data.user_type=data.user_type;
          that.form_data.head_txt=data.head_txt;
          that.form_data.foot_txt=data.foot_txt;
          that.getTitle();
        }
      });
    },
    /*获取课程标题等*/
    getTitle() {
      var that = this;

      that.axios.post("notice/search", {
        type:that.form_data.type,
        kw:this.kw
      }).then(data => {
        this.restaurants=data
        // if(data.status=="success"){
        //   this.restaurants=data.data
        // }else{
        //   this.$message(data.msg)
        // }
      });
    },
    /**
     * 保存预览
     */
    save() {
      const that = this;
      that.$refs.form_data.validate((valid) => {
        if (!valid) return that.$message.warning('请完整填写内容!');
        var formData = {};

        formData.type = that.form_data.type;
        formData.title = that.form_data.title;
        formData.oid = that.form_data.oid;
        formData.user_type = that.form_data.user_type;
        formData.head_txt=that.form_data.head_txt;
        formData.foot_txt=that.form_data.foot_txt;
        var url = '/notice/create';
        if (that.banner_id) {
          formData.id = that.banner_id;
          url = '/notice/update';
        }
        that.axios.post(url, formData).then(() => {

          that.$message.success(that.banner_id ? '修改成功' : '添加成功');
          that.$router.go(-1)
        });
      })
    },
    /**
     * 返回
     */
    back() {
      this.$router.go(-1);
    },
  }
}
